<?php
$code = <<<EOF
package dao

import (
    commonSDK "code.inke.cn/bpc/facetalk/server/sdk/common"
	"code.inke.cn/bpc/facetalk/server/user/{$data['service_name']}/model"
	"context"
	"encoding/json"
	"fmt"
	"git.inke.cn/BackendPlatform/golang/logging"
)

const {$data['model_name']}ItemExpireTime = 86400  //todo 设置过期时间，单位为秒，默认一天，按需修改

//todo 公共方法
//func (d *Dao) HasCache(ctx context.Context, cacheKey string) bool {
//	keyExists, _ := d.redis.Exists(ctx, cacheKey)
//	return keyExists
//}

/**
* 删除缓存//todo 公共方法
 */
//func (d *Dao) delKey(ctx context.Context, key string) error {
//	var (
//		logger = logging.For(ctx, "dao", "delKey", "key", key)
//		err    error
//	)
//
//	_, err = d.redis.Del(ctx, key)
//	if err != nil {
//		logger.Errorf("Del error: %v", err)
//		return err
//	}
//
//	logger.Debugf("success")
//	return nil
//}

/**
 * @Description: 通过ID生成cacheKey
 * @param UID
 * @return string
 */
func Get{$data['model_name']}ItemCacheKey(id int64) string {
	return fmt.Sprintf("{$data['service_name_key']}:{$data['model_name']}:item:%d", id)
}



/**
 * @Description: string类型添加一条数据
 * @param ctx
 * @param uid
 * @param data
 * @return err
 */
func (d *Dao) Add{$data['model_name']}Cache(ctx context.Context, data model.{$data['model_name']}Model, cacheKey string, expireTime int) (err error) {
	logger := logging.For(ctx, "func", "{$data['model_name']}CacheAdd", "data", data)
	//插入数据
	jsonData, err := json.Marshal(data)
	if err != nil {
		logger.Errorf("json.Marshal error, data: %v, err: %v", data, err)
		return
	}

	_, err = d.redis.SetExSecond(ctx, cacheKey, jsonData, expireTime)
	if err != nil {
		logger.Errorf("[redis.SetExSecond] error,data:%v, err:%+v", data, err)
		return
	}
	return nil
}

/**
 * @Description: 获取缓存
 * @param ctx
 * @param uid
 * @param data
 * @return err
 */
func (d *Dao) Get{$data['model_name']}Cache(ctx context.Context, cacheKey string) *model.{$data['model_name']}Model {
	logger := logging.For(ctx, "func", "Gen{$data['model_name']}LatestCache", "cacheKey", cacheKey)
	var record model.{$data['model_name']}Model
	jsonData, err := d.redis.Get(ctx, cacheKey)
	if err != nil {
		logger.Errorf("[redis.Get] error,cacheKey:%v, err:%+v", cacheKey, err)
		return nil
	}
	err = json.Unmarshal(jsonData, &record)
	if err != nil {
		logger.Errorf("[Unmarshal] error,jsonData:%v, err:%+v", jsonData, err)
		return nil
	}
	return &record
}

/** 
 * @Description: zset类型分页信息
 * @param ctx
 * @param offset
 * @param defaultOffset
 * @param cacheKey
 * @return res
 * @return pagination
 */
func (d *Dao) Get{$data['model_name']}ForPagination(ctx context.Context, offset, defaultOffset int, cacheKey string) (res []model.{$data["model_name"]}Model, pagination commonSDK.PaginatorMap) {
    logger := logging.For(ctx, "func", "Get{$data['model_name']}ForPagination", "cacheKey", cacheKey)
	//初始化数据格式，防止返回null这样的数据
	res = make([]model.{$data["model_name"]}Model, 0)

	//计算分页信息
	totalCount, err := d.redis.ZCard(ctx, cacheKey)
	if err != nil {
		logger.Errorf("get totalCount error, cacheKey: %v, err: %v", cacheKey, err)
		return
	}

	pagination = commonSDK.PaginatorForRedis(offset, totalCount, defaultOffset)

	//拿出当前页数据
	resDb, err := d.redis.Zrevrange(ctx, cacheKey, pagination.StartIndex, pagination.EndIndex)

	if err != nil {
		logger.Errorf("get data from redis error or no data ,cacheKey:%v,resDb:%v,error:%v", cacheKey, resDb, err)
		return
	}

	if len(resDb) <= 0 {
		return
	}

	//json转结构体，结构体合并为数组
	for _, val := range resDb {
		var item model.{$data["model_name"]}Model
		str := []byte(val)
		err := json.Unmarshal(str, &item)
		if err != nil {
			logger.Errorf("json.Unmarshal err,json: %v, err: %v", val, err)
			continue
		}
		res = append(res, item)
	}

	return
}

/**
 * @Description: 生成数据列表，用zset结构，create_time作为分数
 * @param ctx
 * @param uid
 * @param data
 * @return err
 */
func (d *Dao) Add{$data['model_name']}ForList(ctx context.Context, record model.{$data['model_name']}Model,cacheKey string) (err error) {
	logger := logging.For(ctx, "func", "Gen{$data['model_name']}ListCache", "record", record)
	//插入数据
	jsonData, err := json.Marshal(record)
	if err != nil {
		logger.Errorf("json.Marshal error, record: %v, err: %v", record, err)
		return err
	}
	score := record.CreateTime.Unix()
	_, err = d.redis.ZAdd(ctx, cacheKey, score,jsonData)
	if err != nil {
		logger.Errorf("[redis.ZAdd] error,record:%v, err:%+v", record, err)
		return
	}
	return nil
}

/**
 * @Description: 批量插入zset列表
 * @param ctx
 * @param uid
 * @param data
 * @return err
 */
func (d *Dao) BatchAdd{$data['model_name']}ForList(ctx context.Context, records []model.{$data['model_name']}Model, cacheKey string) (err error) {
	logger := logging.For(ctx, "func", "BatchAdd{$data['model_name']}ForList", "record", records)
	//制作数据
	data := make([]interface{}, 0)
	for _, v := range records {
		jsonData, err := json.Marshal(v)
		if err != nil {
			logger.Errorf("json.Marshal error, record: %v, err: %v", records, err)
			continue
		}
		data = append(data, v.Score)
		data = append(data, jsonData)
	}

	_, err = d.majiaCommonRedis.ZAdd(ctx, cacheKey, data...)
	if err != nil {
		logger.Errorf("[redis.ZAdd] error,record:%v, err:%+v", records, err)
		return
	}
	return nil
}

EOF;
?>
<?= $code?>
